package com.ruoyi.typing.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.typing.domain.Student;
import com.ruoyi.typing.service.IStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生Controller
 * 
 * @author ruoyi
 * @date 2021-12-22
 */
@Controller
@RequestMapping("/typing/student")
public class StudentController extends BaseController
{
    private String prefix = "typing/student";

    @Autowired
    private IStudentService studentService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("typing:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询学生列表
     */
    @RequiresPermissions("typing:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 导出学生列表
     */
    @RequiresPermissions("typing:student:export")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        return util.exportExcel(list, "学生数据");
    }

    /**
     * 新增学生
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/batchAdd")
    public String batchAdd()
    {
        return prefix + "/batch_add";
    }

    /**
     * 新增保存学生
     */
    @RequiresPermissions("typing:student:add")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Student student)
    {
        student.setActive(1);
        student.setCishu(0L);
        SysDept sysDept1 = this.deptService.selectDeptById(student.getJiaoyujuId());
        SysDept sysDept2 = this.deptService.selectDeptById(student.getJituanxiaoId());
        SysDept sysDept3 = this.deptService.selectDeptById(student.getSchoolId());
        student.setJiaoyuju(sysDept1.getDeptName());
        student.setJituanxiao(sysDept2.getDeptName());
        student.setSchool(sysDept3.getDeptName());

        return toAjax(studentService.insertStudent(student));
    }

    @RequiresPermissions("typing:student:add")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    @ResponseBody
    public AjaxResult batchAddSave(Student student)
    {
        student.setActive(1);
        student.setCishu(0L);
        SysDept sysDept1 = this.deptService.selectDeptById(student.getJiaoyujuId());
        SysDept sysDept2 = this.deptService.selectDeptById(student.getJituanxiaoId());
        SysDept sysDept3 = this.deptService.selectDeptById(student.getSchoolId());
        student.setJiaoyuju(sysDept1.getDeptName());
        student.setJituanxiao(sysDept2.getDeptName());
        student.setSchool(sysDept3.getDeptName());

        String[] stuList = student.getName().split("\r\n");
        for(String name : stuList){
            if(name.isEmpty()){
                continue;
            }
            Student stu = new Student();

            stu.setJiaoyujuId(student.getJiaoyujuId());
            stu.setJituanxiaoId(student.getJituanxiaoId());
            stu.setSchoolId(student.getSchoolId());

            stu.setNianji(student.getNianji());
            stu.setBanji(student.getBanji());
            stu.setName(name);

            List<Student> students = this.studentService.selectStudentList(stu);
            if(students.size()==0){
                stu.setActive(1);
                stu.setCishu(0L);

                stu.setJiaoyuju(student.getJiaoyuju());
                stu.setJituanxiao(student.getJituanxiao());
                stu.setSchool(student.getSchool());

                this.studentService.insertStudent(stu);
            }
        }

        return AjaxResult.success();
    }

    /**
     * 修改学生
     */
    @GetMapping("/edit/{studentId}")
    public String edit(@PathVariable("studentId") Long studentId, ModelMap mmap)
    {
        Student student = studentService.selectStudentByStudentId(studentId);
        mmap.put("student", student);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生
     */
    @RequiresPermissions("typing:student:edit")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生
     */
    @RequiresPermissions("typing:student:remove")
    @Log(title = "学生", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(studentService.deleteStudentByStudentIds(ids));
    }

    @PostMapping("/listSysDept")
    @ResponseBody
    public List<SysDept> listSysDept(@RequestBody SysDept dept)
    {
        List<SysDept> deptList = deptService.selectDeptList(dept);
        return deptList;
    }

    @PostMapping("/checkStudentExist")
    @ResponseBody
    public AjaxResult checkStudentExist(@RequestBody Student student)
    {
        List<Student> students = this.studentService.selectStudentList(student);
        if(students.size()==0){
            return AjaxResult.error("学生不存在");
        }
        else {
            return AjaxResult.success();
        }
    }

}
